package com.wins.bbs.auth;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;

import com.wins.bbs.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

public class JwtAuthentication implements Authentication {

    private User user;
    private final String token;
    private final Map<String, Object> detail;

    public JwtAuthentication(final User user, final String token, final Map<String, Object> detail) {
        this.user = user;
        this.token = token;
        this.detail = detail;
    }

    public Optional<User> user() {
        return Optional.ofNullable(user);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.emptyList();
    }

    @Override
    public Object getCredentials() {
        return token;
    }

    @Override
    public Object getDetails() {
        return detail;
    }

    @Override
    public Object getPrincipal() {
        return user;
    }

    @Override
    public boolean isAuthenticated() {
        return user.getPassword() != null;
    }

    @Override
    public void setAuthenticated(final boolean b) throws IllegalArgumentException {
        if (!isAuthenticated()) {
            user = null;
        }
    }

    @Override
    public String getName() {
        return user.getLoginId();
    }
}
